package com.goods.business.service.imp;

import com.goods.business.mapper.ProductSupplierMapper;
import com.goods.business.service.ProductSupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class ProductSupplierServiceImpl implements ProductSupplierService {

    @Autowired
    private ProductSupplierMapper productSupplierMapper;
    /**
     *
     * @param pageNum
     * @param pageSize
     * @param supplier
     * @return
     */
    @Override
    public List<Supplier> findSupplierList(Integer pageNum, Integer pageSize, Supplier supplier) {
        //
        if (supplier.getAddress()==null&&supplier.getName()==""&&supplier.getContact()==null) {
            List<Supplier> suppliers = productSupplierMapper.selectAll();
            return suppliers;
        }else {
            Example example = new Example(supplier.getClass());
            example.createCriteria()
                    .andLike("name", supplier.getName()+"%")
                    .andLike("address", supplier.getAddress()+"%")
                    .andLike("contact", supplier.getContact()+"%");
            //TODO:bug->address未分割。
            List<Supplier> suppliers = productSupplierMapper.selectByExample(example);
            return suppliers;
        }
    }

    @Override
    public void add(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        productSupplierMapper.insert(supplier);
    }


    @Override
    public Supplier edit(Long id) {
        //
        Supplier supplier = productSupplierMapper.selectByPrimaryKey(id);
        return supplier;
    }

    @Override
    public void update(Long id, Supplier supplier) {
        Supplier ss = new Supplier();
        BeanUtils.copyProperties(supplier,ss);
        ss.setModifiedTime(new Date());
        productSupplierMapper.updateByPrimaryKey(ss);

    }
}
